package com.lx.day0328;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @author c先生
 * @date 2022年03月28日 下午 7:02:18
 * @describe
 */

public class Util {
    ArrayList<Student> als = new ArrayList<Student>();
    Scanner sc = new Scanner(System.in);
    public Util() {
        this.init();
    }


    //根据学号查询一名学生的信息
    public void findStudent(int id){
        Iterator it = als.iterator();//获取Iterator对象
        while(it.hasNext()){//hasNext()  判断下一个索引位置是否还有元素
            Object next = it.next();//next() 获取索引位置的元素 并且指针下移
            Student stu = (Student)next;
            if(stu.getId()==id) {
                System.out.println(next.toString());
                return;
            }
        }
    }

    //根据学号修改一名学生信息
    public boolean updateStudent(int id){
        Iterator it = als.iterator();//获取Iterator对象
        while(it.hasNext()){//hasNext()  判断下一个索引位置是否还有元素
            Object next = it.next();//next() 获取索引位置的元素 并且指针下移
            Student stu = (Student)next;
            if(stu.getId()==id){
                als.set(als.indexOf(next),this.inputStudentInfo(id));
                return true;
            }
        }
        return false;
    }
    //根据学号删除一名学生
    public boolean delStudent(int id){
        Iterator it = als.iterator();//获取Iterator对象
        while(it.hasNext()){//hasNext()  判断下一个索引位置是否还有元素
            Object next = it.next();//next() 获取索引位置的元素 并且指针下移
            Student stu = (Student)next;
            if(stu.getId()==id){
                als.remove(next);
                return true;
            }
        }
        return false;
    }

    //添加一名学生
    public boolean addStudent(Student student){
        return  als.add(student);
    }

    //添加学生信息
    public Student inputStudentInfo(){
        System.out.println("请输入学号：");
        int id = sc.nextInt();
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入班级：");
        String banji = sc.next();
        System.out.println("请输入年龄：");
        int age = sc.nextInt();
        System.out.println("请输入性别：");
        String sex = sc.next();
        System.out.println("请输入成绩：");
        int grade = sc.nextInt();
        Student student = new Student(id,name,banji,age,sex,grade);
        return student;
    }
    public Student inputStudentInfo(int id) {
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入班级：");
        String banji = sc.next();
        System.out.println("请输入年龄：");
        int age = sc.nextInt();
        System.out.println("请输入性别：");
        String sex = sc.next();
        System.out.println("请输入成绩：");
        int grade = sc.nextInt();
        Student student = new Student(id, name, banji, age, sex, grade);
        return student;
    }
    //展示所有学生的信息
    public void showAll(){
        Iterator it = als.iterator();//获取Iterator对象
        while(it.hasNext()){//hasNext()  判断下一个索引位置是否还有元素
            Object next = it.next();//next() 获取索引位置的元素 并且指针下移
            System.out.println(next.toString());
        }
//        boolean empty = als.isEmpty();//判断集合是否为空
    }



    //初始化集合中的元素
    public void init(){
        Student stu1 = new Student(01,"李欣","T09",20,"男",100);
        Student stu2 = new Student(02,"周煜民","T09",21,"女",90);
        Student stu3 = new Student(03,"杨威","T09",22,"男",80);
        als.add(stu1);
        als.add(stu2);
        als.add(stu3);
    }


}
